package com.xmut.bs.handaccount.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.widget.Checkable;

import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.util.DisplayUtils;


/**
 * Author Dollkey
 * Date 2017/12/22 10:14.
 */

public class TagTextView extends AppCompatTextView implements Checkable {

    /**
     * 圆角大小
     */
    private float mCornerSize;
    private float mStrokeWidth;
    private int mCheckBackgroundColor;
    private int mNormalBackgroundColor;
    private int mCheckTextColor;
    private int mNormalTextColor;
    private String mText;

    private Paint mBackgroundPaint;
    private Paint mTextPaint;
    private RectF rec;

    private boolean isChecked;
    private boolean isInvalidate;
    private Context mContext;

    public TagTextView(Context context) {
        this(context, null);
    }

    public TagTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TagTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TagTextView, defStyleAttr, 0);
        //通过TypedArray提供的一系列方法getXXXX取得我们在xml里定义的参数值；
        // 获取圆角大小
        mCornerSize = a.getDimensionPixelSize(R.styleable.TagTextView_cornerBgSize, 10);
        mCheckBackgroundColor = a.getColor(R.styleable.TagTextView_checkBackgroundColor, context.getResources().getColor(R.color.colorPrimary));
        mNormalBackgroundColor = a.getColor(R.styleable.TagTextView_normalBackgroundColor, Color.WHITE);
        mCheckTextColor = a.getColor(R.styleable.TagTextView_checkTextColor, Color.WHITE);
        mNormalTextColor = a.getColor(R.styleable.TagTextView_normalTextColor, context.getResources().getColor(R.color.colorPrimary));
        mText = a.getString(R.styleable.TagTextView_tagText);
        mStrokeWidth = a.getDimensionPixelSize(R.styleable.TagTextView_tagStrokeWidth, DisplayUtils.dp2px(mContext,1));
        isChecked = a.getBoolean(R.styleable.TagTextView_tagChecked, false);
        isInvalidate = false;
        //调用 recycle() 回收TypedArray,以便后面重用
        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBackgroundPaint == null) {
            mBackgroundPaint = new Paint();
            mBackgroundPaint.setAntiAlias(true);
            mBackgroundPaint.setStrokeWidth(mStrokeWidth);
        }
        if (rec == null || isInvalidate){
            rec = new RectF(mStrokeWidth / 2, mStrokeWidth / 2, getMeasuredWidth() - mStrokeWidth / 2,
                    getMeasuredHeight() - mStrokeWidth / 2);
            isInvalidate = false;
        }
        mBackgroundPaint.setStyle(isChecked ? Paint.Style.FILL : Paint.Style.STROKE);
        mBackgroundPaint.setColor(mCheckBackgroundColor);
        //        mBackgroundPaint.setColor(isChecked ? mCheckBackgroundColor : mNormalBackgroundColor);
        canvas.drawRoundRect(rec, mCornerSize, mCornerSize, mBackgroundPaint);
        setTextColor(isChecked ? mCheckTextColor : mNormalTextColor);
        super.onDraw(canvas);
    }

    public void setCornerSize(float cornerSize) {
        mCornerSize = cornerSize;
    }

    public void setStrokeWidth(float strokeWidth) {
        mStrokeWidth = strokeWidth;
    }

    public void setCheckBackgroundColor(int checkBackgroundColor) {
        mCheckBackgroundColor = checkBackgroundColor;
    }

    public void setNormalBackgroundColor(int normalBackgroundColor) {
        mNormalBackgroundColor = normalBackgroundColor;
    }

    public void setCheckTextColor(int checkTextColor) {
        mCheckTextColor = checkTextColor;
    }

    public void setNormalTextColor(int normalTextColor) {
        mNormalTextColor = normalTextColor;
    }

    public void setTagText(CharSequence text) {
        setText(text);
        isInvalidate = true;
        invalidate();
    }

    @Override
    public void setChecked(boolean checked) {
        isChecked = checked;
        isInvalidate = true;
        invalidate();
    }

    @Override
    public boolean isChecked() {
        return isChecked;
    }

    @Override
    public void toggle() {
        setChecked(!isChecked);
    }
}
